/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.data.collections.Map;
import com.bourre.events.BasicEvent;
import com.bourre.ioc.model.AbstractModel;

import fever.ioc.events.PluginEvent;
import fever.ioc.manager.events.PluginManagerEventList;
import fever.ioc.manager.events.PluginManagerListener;
import fever.ioc.manager.models.ModelList;
import fever.ioc.plugin.FeverPlugin;
import fever.ioc.plugin.PluginInfo;

/**
 * Plugin manager model.
 * 
 * <p>Plugins are registred / activated using their {@code communication 
 * channel} property.
 * 
 * @see fever.ioc.manager.events.PluginManagerEventList
 * 
 * @author Romain Ecarnot
 */
class fever.ioc.manager.models.PluginManagerModel extends AbstractModel
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _pluginMap : Map;
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * 
	 * @param owner FeverPlugin owner.
	 */ 
	public function PluginManagerModel( owner : FeverPlugin ) 
	{
		super( owner, ModelList.PLUGIN_MANAGER_MODEL );
		
		_pluginMap = new Map();
	}	
	
	/**
	 * Returns plugin owner.
	 */
	public function getOwner(  ) : FeverPlugin
	{
		return FeverPlugin( _owner );
	}
	
	/**
	 * Adds passed-in {@code listener} object for receiving all model 
	 * events.
	 * 
	 * @param listener Listener object
	 */
	public function addListener( listener : PluginManagerListener ) : Void
	{
		super.addListener( listener );
	}
	
	/**
	 * Removes passed-in {@code listener} object for receiving all model 
	 * events.
	 * 
	 * @param listener Listener object.
	 */
	public function removeListener( listener : PluginManagerListener ) : Void
	{
		super.removeListener( listener );
	}
	
	/**
	 * Registers a new plugin using passed-in plugin information.
	 * 
	 * <p>Only store plugin information, not plugin instance itself.<br />
	 * Uses a data collection to store informations.<br />
	 * Plugin are activated using their communication channel id.
	 * 
	 * <p>Connected view are notified. ( MVC rule )
	 * 
	 * @param {@link fever.ioc.plugin.PluginInfo} instance
	 */
	public function register( info : PluginInfo ) : Void
	{
		if( !_pluginMap.containsKey( info.channel ) )
		{
			_pluginMap.put( info.channel, info );
			
			notifyChanged( 
				new PluginEvent( 
					PluginManagerEventList.onRegisterPluginEVENT, 
					info 
				) 
			);
		}
	}
	
	/**
	 * Activates plugin using passed-in {@code channel}. 
	 * 
	 * <p>Fire an external event on {@code channel} plugin.
	 * 
	 * <p>Connected view are notified. ( MVC rule )
	 * 
	 * @see PluginManagerEventList.onActivatePluginEVENT
	 * 
	 * @param channel Plugin channel to activate
	 */
	public function activate( channel : String ) : Void
	{
		if( _pluginMap.containsKey( channel ) )
		{
			getOwner().fireExternalEvent( 
				new BasicEvent( 
					PluginManagerEventList.onActivatePluginEVENT 
				), 
				channel 
			);
			
			notifyChanged( 
				new PluginEvent( 
					PluginManagerEventList.onActivatePluginEVENT, 
					_pluginMap.get( channel ) 
				) 
			);
		}
		else getLogger().error( 
			channel + ' plugin is not available in context' 
		);
	}
}